package com.heyun.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heyun.bean.HeyunChildMenu;
import com.heyun.bean.HeyunDept;
import com.heyun.bean.HeyunMenu;
import com.heyun.bean.HeyunRole;
import com.heyun.bean.HeyunRoleMenu;
import com.heyun.service.HeyunChildMenuService;
import com.heyun.service.impl.HeyunDeptImpl;
import com.heyun.service.impl.HeyunRoleImpl;

/**
 * @author 成栋梁
 *
 */
@RequestMapping("/dept")
@Controller
public class DeptController {
    @Autowired
    HeyunChildMenuService hcms;
   @Autowired
   HeyunDeptImpl hydi;
   @Autowired
   HeyunRoleImpl hyri;
   /**
    * @author 成栋梁
    * 增加新部门
    */
   @RequestMapping("/saveDept")
   @ResponseBody
   public Map saveDept(HeyunDept hyd){
	   Map map=new HashMap();
	     int a=  hydi.insertDept(hyd);
	     if(a>0){
	    	 map.put("message", "添加成功");
	     }else{
	    	 map.put("message", "页面出错，请重新添加");
	     }
	   return map;
   }
   /**
    * @author 成栋梁
    * 获取dept页面单个部门内容
    */
   @RequestMapping("/getDeptName")
   @ResponseBody
   public Map getDeptName(String deptId){
	   Map map=new HashMap();
	   HeyunDept dept = hydi.getDept(Integer.valueOf(deptId));
	   map.put("dept", dept);
	   return map;
   }
   /**
    * @author 成栋梁
    * 更新页面部门
    */
   @RequestMapping("/updateDept")
   @ResponseBody
   public Map updateDept(HeyunDept hyd){
	   Map map =new HashMap();
	   int a=hydi.updateDept(hyd);
	   if(a>0){
	    	 map.put("message", "更新成功");
	     }else{
	    	 map.put("message", "页面出错，请重新操作");
	     }
	   return map;
   }
   /**
    * @author 成栋梁
    *删除部门
    */
   @RequestMapping("/deleteDept")
   @ResponseBody
   public Map deleteDept(String deptId){
	   Map map =new HashMap();
	   int a=hydi.removeDept(Integer.valueOf(deptId));
	   List<HeyunRole> list= hyri.listDeptRole(Integer.valueOf(deptId));
	   for (HeyunRole l:list) {
		hyri.removeHeyunRole(l.getRoleId());
	  }
	   if(a>0){
	    	 map.put("message", "删除成功");
	     }else{
	    	 map.put("message", "页面出错，请重新操作");
	     }
	   return map;
   }
   @RequestMapping("/listDept")
   @ResponseBody
   public Map listDept(){
	   List<HeyunDept>list=hydi.listAllDept();
	   Map map=new HashMap();
	   map.put("deptList", list);
	   return map;
   }
   /**
    * @author 成栋梁
    *   保存新建的角色
    */
   @RequestMapping("/saveRole")
   @ResponseBody
   public Map saveRole(String roleName,String deptId,String roleRemarks){
	   Map map=new HashMap();
	   HeyunRole hyr=new HeyunRole();
	   hyr.setRoleName(roleName);
	   hyr.setRoleRemarks(roleRemarks);
	   hyr.setDeptId(Integer.valueOf(deptId));
	    int a=hyri.insertRole(hyr);
	    
	    if(a>0){
	      map.put("message", "添加成功");	
	    }else{
	      map.put("message", "添加失败");	
	    }
	   return map;
   }
   /**
    * @author 成栋梁
    * 获取所有角色信息
    */
   @RequestMapping("/getListRole")
   @ResponseBody
   public Map getListRole(){
	   Map map=new HashMap();
	   List<HeyunDept> deptList=hydi.listAllDept();
	   List<HeyunRole>roleList=hyri.listRole();
	   map.put("deptList", deptList);
	   map.put("roleList", roleList);
	   return map;
   }
   /**
    * @author 成栋梁
    *获取单个角色信息
    */
   @RequestMapping("/getRoleMessage")
   @ResponseBody
   public Map getRoleMessage(String roleId){
	   Map map=new HashMap(); 
	   System.out.println(roleId);
	   HeyunRole hyr= hyri.getRole(Integer.parseInt(roleId));
	   List<HeyunDept> deptList=hydi.listAllDept();
	   map.put("role", hyr);
	   map.put("deptList", deptList);
	   return map;
   }
   /**
    * @author 成栋梁
    * 删除角色
    */
   @RequestMapping("/deleteRole")
   @ResponseBody
   public Map deleteRole(String roleId){
	   Map map=new HashMap(); 
	   System.out.println(roleId);
	   int a=hyri.removeHeyunRole(Integer.valueOf(roleId));
	   if(a>0){
		   map.put("message", "删除成功");
	   }else{
		   map.put("message", "系统出错，请重新操作");
	   }
	   return map;
   }
   /**
    * @author 成栋梁
    *更新角色信息
    */
   @RequestMapping("/updateRole")
   @ResponseBody
   public Map updateRole(HeyunRole hyr){
	   Map map=new HashMap(); 
	   System.out.println(hyr);
	   int a=hyri.updateRole(hyr);
	   if(a>0){
		   map.put("message", "更新成功");
	   }else{
		   map.put("message", "系统出错，请重新操作");
	   }
	   return map;
   }
   /**
    * @author 成栋梁
    *  获取管理一二级菜单
    */
   @RequestMapping("/getManageMenu")
   @ResponseBody
   public Map getManageMenu(String roleId){
	   Map map=new HashMap();
	     List<HeyunMenu> listMenu = hcms.listMenu();
	     List<HeyunChildMenu> listAllChildMenu = hcms.listAllChildMenu();
	     List<HeyunRoleMenu> listRolePower = hcms.listRolePower(Integer.valueOf(roleId));
	     map.put("listMenu", listMenu);
	     map.put("listAllChildMenu", listAllChildMenu);
	     map.put("listRolePower", listRolePower);
	   return map;
   }
   /**
    * @author 成栋梁
    * 获取前端 超级管理员分配的权限的内容
    * 此处逻辑较为复杂
    * 如果前端的获取的值 数据库中没有，就添加
    * 如果前端获取的东西 后端有，不用添加，
    * 后端存在的内容，前端不存在，则要执行删除操作
    */
   @RequestMapping("/givePower")
   @ResponseBody
   public Map givePower(String roleId,HttpServletRequest request){
	
	   System.out.println(request.getParameter("attr"));
	   String attr=request.getParameter("attr");
	   String []childMenu=attr.split(",");
	   for (int i = 0; i < childMenu.length; i++) {
		   System.out.println(childMenu[i]);
	}
	  

	   
	Map map=new HashMap();
	   int a =Integer.valueOf(roleId);
	   System.out.println("****角色Id****"+a);
	   List <Integer>newList =new ArrayList();
	   for (int i = 0; i < childMenu.length; i++) {
		   newList.add(Integer.valueOf(childMenu[i])); 
	     }
	   List<HeyunRoleMenu> listRolePower = hcms.listRolePower(a);
	   if(listRolePower.isEmpty()){
		  for(Integer l:newList){
			  HeyunRoleMenu hr=new HeyunRoleMenu();
	    		hr.setRoleId(a);
	    		hr.setChildMenuId(l);
	    		hcms.giveNewPower(hr);
	    		map.put("message","新用户添加功能");
		  }
		   
	     }else{
	    System.out.println(listRolePower.toString());
	    List <Integer>list=new ArrayList<Integer>();
	    for(HeyunRoleMenu l:listRolePower){
	    	list.add(l.getChildMenuId());
	    }
	    System.out.println(list.toString());
	    for(Integer l:list){
	    	HeyunRoleMenu hr=new HeyunRoleMenu();
    		hr.setRoleId(a);
    		hr.setChildMenuId(l);
    		hcms.removePower(hr);
    		System.out.println("删除功能");	
	    }
	    for(Integer ll:newList){
	    	HeyunRoleMenu hrs=new HeyunRoleMenu();
			hrs.setRoleId(a);
			hrs.setChildMenuId(ll);
			hcms.giveNewPower(hrs);
			System.out.println("增加新功能");
			map.put("message", "旧角色添加新功能");
	    }
	   /* for(Integer l:list){	
	    	if(!newList.contains(l)){
	    		HeyunRoleMenu hr=new HeyunRoleMenu();
	    		hr.setRoleId(a);
	    		hr.setChildMenuId(l);
	    		hcms.removePower(hr);
	    		System.out.println("删除功能");		
	    	}
	    	for(Integer ll:newList){
	    		if(!list.contains(ll)){
	    			HeyunRoleMenu hrs=new HeyunRoleMenu();
	    			hrs.setRoleId(a);
	    			hrs.setChildMenuId(ll);
	    			hcms.giveNewPower(hrs);
	    			System.out.println("增加新功能");
	    			map.put("message", "旧角色添加新功能");
	    		}}
	    	*/
	          
	    
	   }
	   return map;
   }
  
}
